//
//  ViewController.m
//  TYUIKit_Example
//
//  Created by 谢航宇_WORK on 2022/1/11.
//  Copyright © 2022 谢航宇. All rights reserved.
//

#import "ViewController.h"
#import "TYCarouselView.h"
@interface ViewController ()<TYCarouselViewDelegate>
@property (nonatomic, strong) TYCarouselView *carouselView;
@property (nonatomic, strong) NSArray * dataSource;
@end

@interface HomeCarouselChildCell : UICollectionViewCell
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
}

- (void)initUI {
    __weak typeof(self) weakSelf = self;
    self.carouselView = [[TYCarouselView alloc]init];
    self.carouselView.delegate = self;
    self.carouselView.layer.cornerRadius = 8 ;
    self.carouselView.layer.masksToBounds = YES;
    self.carouselView.backgroundColor = [UIColor whiteColor]; 
    self.carouselView.currentIndex = 0;
    self.carouselView.timeInterval = 3;
    self.carouselView.frame = CGRectMake(0 , 0, 412, 351); 
    [self.carouselView registerClass:[HomeCarouselChildCell class] forCellWithReuseIdentifier:@"HomeCarouselChildCell"];
    [self.view addSubview:self.carouselView];
    [self.carouselView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view).offset(100);
        make.height.mas_equalTo(351);
    }];
    
    _dataSource = @[@"somago-2",@"somago-3",@"somago-4",@"somago-5",@"somago-6",@"somago-7"];
    [self.carouselView reloadData];
}
 

- (UICollectionViewCell *)carouselView:(TYCarouselView *)carouselView cellForItemAtIndex:(NSInteger)index {
    HomeCarouselChildCell * cell = [self.carouselView dequeueReusableCellWithReuseIdentifier:@"HomeCarouselChildCell" forIndex:index];
    cell.imageView.backgroundColor = @[[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor orangeColor],[UIColor systemPinkColor],[UIColor cyanColor]][index];
    return cell;
}

- (NSInteger)numberOfItemsForCarouselView:(TYCarouselView *)carouselView {
    return _dataSource.count;
}

- (void)carouselView:(TYCarouselView *)carouselView scrollToIndex:(NSInteger)index {
    
}
 
- (void)carouselView:(TYCarouselView *)carouselView didSelectedAtIndex:(NSInteger)index {
    NSLog(@"点击图片");
}

@end


@implementation HomeCarouselChildCell
- (UIImageView *)imageView {
    if (!_imageView) {
        __weak typeof(self) weakSelf = self;
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
//        _imageView.layer.cornerRadius = 8 * SCALE_X;
//        _imageView.layer.masksToBounds = YES;
        [self addSubview:_imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.top.bottom.mas_equalTo(weakSelf);
        }];
    }
    return _imageView;
}
@end
